package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.category.CategoryTeaBrandFilterActivity.BrandName;
import cn.com.dhc.danlu.util.CommonUtils;

/**
 * 筛选列表的适配器
 */
public class BrandNameListAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<BrandName> stringList;

    /**
     * 判断是否排序
     */
    private boolean sort = false;
    private int selectedIndex = -1;

    public BrandNameListAdapter(Context context, List<BrandName> arr, boolean sort) {
        layoutInflater = LayoutInflater.from(context);
        stringList = arr;
        this.sort = sort;
    }

    public void setSelectedIndex(int index) {
        this.selectedIndex = index;
    }

    public int getSelectIndex() {
        return this.selectedIndex;
    }

    @Override
    public int getCount() {
        return stringList == null ? 0 : stringList.size();
    }

    @Override
    public Object getItem(int position) {
        return stringList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.view_brand_list_item, null);
            holder = new ViewHolder();
            holder.firstCharHintTextView = (TextView) convertView.findViewById(R.id.txtCharacter);
            holder.txtBrandName = (TextView) convertView.findViewById(R.id.txtName);
            holder.imgLine = (ImageView) convertView.findViewById(R.id.imgLine);
            holder.txtImg = (ImageView) convertView.findViewById(R.id.txtImg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (selectedIndex == position) {
            // 选中
            holder.txtImg.setVisibility(View.VISIBLE);
            holder.txtBrandName.setTextColor(Color.RED);
        } else {
            // 未选中
            holder.txtImg.setVisibility(View.GONE);
            holder.txtBrandName.setTextColor(Color.BLACK);
        }

        BrandName dataItem = (BrandName) getItem(position);
        if (dataItem == null) {
            return convertView;
        }

        holder.txtBrandName.setText(dataItem.getName());
        if (sort) {
            int previewIdx = position - 1;
            int nextIdx = position + 1;
            char previewChar = previewIdx >= 0 ? CommonUtils.getFirstLetter(stringList.get(previewIdx).getPinYin().toUpperCase()).charAt(0) : ' ';
            char nextChar = nextIdx <= getCount() - 1 ? CommonUtils.getFirstLetter(stringList.get(nextIdx).getPinYin().toUpperCase()).charAt(0) : ' ';

            char currentChar = CommonUtils.getFirstLetter(stringList.get(position).getPinYin().toUpperCase()).charAt(0);

            if (currentChar != previewChar) {
                holder.firstCharHintTextView.setVisibility(View.VISIBLE);
                holder.firstCharHintTextView.setText(String.valueOf(currentChar).toUpperCase());
            } else {
                holder.firstCharHintTextView.setVisibility(View.GONE);
            }

            if (currentChar != previewChar && currentChar == nextChar) {
                holder.imgLine.setVisibility(View.VISIBLE);
            } else if (currentChar == previewChar && currentChar == nextChar) {
                holder.imgLine.setVisibility(View.VISIBLE);
            } else if (currentChar == previewChar && currentChar != nextChar) {
                holder.imgLine.setVisibility(View.GONE);
            } else {
                holder.imgLine.setVisibility(View.GONE);
            }
            //隐藏全部品牌字母
            if (position == 0) {
                holder.firstCharHintTextView.setVisibility(View.GONE);
            }
        } else {
            holder.firstCharHintTextView.setVisibility(View.GONE);
        }

        return convertView;
    }

    public static class ViewHolder {
        ImageView txtImg;
        TextView firstCharHintTextView;
        TextView txtBrandName;
        ImageView imgLine;
    }
}